﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RoketPack.Management
{
    public class InterProcessHandle : MarshalByRefObject
    {
        public static bool ApplicationIsInstalled(object refmanager, string repository, string application)
        {
            Manager manager = (Manager)refmanager;

            foreach (Repository r in manager.Repositories)
            {
                try
                {
                    string url = r.Url;
                    if (url == repository.TrimEnd('/'))
                        return r.Applications[application].IsInstalled();
                }
                catch (KeyNotFoundException)
                {
                }
            }

            return false;
        }

        public static bool ComponentIsInstalled(object refmanager, string repository, string application, string component)
        {
            Manager manager = (Manager)refmanager;

            foreach (Repository r in manager.Repositories)
            {
                try
                {
                    if (r.Url == repository.TrimEnd('/'))
                    {
                        if (r.Applications[application].Components[component].Available == false)
                            return false;
                        return r.Applications[application].Components[component].IsInstalled();
                    }
                }
                catch (KeyNotFoundException)
                {
                }
            }

            return false;
        }

        public static bool ComponentInstall(object refmanager, string repository, string application, string component)
        {
            Manager manager = (Manager)refmanager;

            // TODO: Use the yet-to-be-defined roket protocol class to handle these
            // API requests directly.
            return InterProcessHandle.MiscellaneousCallURL(refmanager, "roket:install?repository=" + repository + "&application=" + application + "&component=" + component);
        }

        public static bool ComponentUpdate(object refmanager, string repository, string application, string component)
        {
            Manager manager = (Manager)refmanager;

            // TODO: Use the yet-to-be-defined roket protocol class to handle these
            // API requests directly.
            return InterProcessHandle.MiscellaneousCallURL(refmanager, "roket:update?repository=" + repository + "&application=" + application + "&component=" + component);
        }

        public static bool ComponentUninstall(object refmanager, string repository, string application, string component)
        {
            Manager manager = (Manager)refmanager;

            // TODO: Use the yet-to-be-defined roket protocol class to handle these
            // API requests directly.
            return InterProcessHandle.MiscellaneousCallURL(refmanager, "roket:uninstall?repository=" + repository + "&application=" + application + "&component=" + component);
        }

        public static bool VersionIsLatest(object refmanager, string repository, string application, string component)
        {
            Manager manager = (Manager)refmanager;

            foreach (Repository r in manager.Repositories)
            {
                if (r.Applications == null)
                    continue;

                try
                {
                    if (r.Url == repository.TrimEnd('/'))
                    {
                        if (r.Applications[application].Components[component].Available == false)
                            return false;
                        return !r.Applications[application].Components[component].IsOutOfDate();
                    }
                }
                catch (KeyNotFoundException)
                {
                }
            }

            return true;
        }

        public static bool MiscellaneousCallURL(object refmanager, string url)
        {
            Manager manager = (Manager)refmanager;

            return manager.RemoteURLInvoke(url, false);
        }

        public static bool AuthorizedCallURL(object refmanager, string url)
        {
            Manager manager = (Manager)refmanager;

            return manager.RemoteURLInvoke(url, true);
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
    }
}
